package com.boco.ibssystem.business.service.config.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boco.biinjectionserviceapi.model.config.SqlTemplateInfo;
import com.boco.biinjectionserviceapi.service.config.ISqlTemplateInfoService;
import com.boco.biinjectionserviceapi.vo.Params;
import com.boco.ibssystem.business.mapper.config.SqlTemplateInfoMapper;
import com.boco.ibssystem.common.provider.DataProvider;
import com.boco.ibssystem.common.provider.DataProviderManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.boco.ibssystem.common.utils.DateUtil.*;

@Service
public class SqlTemplateInfoServiceImpl extends ServiceImpl<SqlTemplateInfoMapper, SqlTemplateInfo> implements ISqlTemplateInfoService {

    @Autowired
    private DataProviderManager dataProviderManager;

    @Override
    @Deprecated
    public Map<String, Float> queryByParams(long id, long id2,Params params) throws Exception {
        Map<String, Float> map = new HashMap<>();
        //查询出参数进行简单拼接
        SqlTemplateInfo info = super.getById(id);
        StringBuilder sqlTemplate = new StringBuilder();
        DataProvider dataProvider = dataProviderManager.getDataProvider(info.getDataSourceConfigId());
        if (params.getQuYuCode() != null) {
            //TODO 关联区域代码
            // sqlTemplate.append(" and quyucode = "+params.getQuYuCode());
        }
        if (params.getStartTime() != null) {
            sqlTemplate.append(" and YWSCSJ > to_date('" + params.getStartTime() + "','yyyy-MM-dd')");
        } else {
            sqlTemplate.append(" and YWSCSJ > to_date('" + formatDate(getCurrYearFirst()) + "','yyyy-MM-dd')");
        }
        if (params.getEndTime() != null) {
            sqlTemplate.append(" and YWSCSJ < to_date('" + params.getEndTime() + "','yyyy-MM-dd')");
        } else {
            sqlTemplate.append(" and YWSCSJ < to_date('" + formatDate(getCurrYearLastDay()) + "','yyyy-MM-dd')");
        }
        if (params.getJiGouCode() != null) {
            sqlTemplate.append(" and YLJGDM = " + params.getJiGouCode());
            System.out.println(sqlTemplate.toString());
            Float feiYong = dataProvider.getFeiYong(info.getSqlTemplate()+sqlTemplate.toString());
            map.put("AllFeiYong",feiYong);
            map.put(params.getJiGouCode().toString(),feiYong);
        }else{
            System.out.println(sqlTemplate.toString());
            Float feiYong = dataProvider.getFeiYong(info.getSqlTemplate()+sqlTemplate.toString());
            map.put("AllFeiYong",feiYong);
            SqlTemplateInfo info2 = super.getById(id2);
            Map<String, Float> feiYongByGroup = dataProvider.getFeiYongByGroup(info2.getSqlTemplate() + sqlTemplate.toString() + " GROUP BY YLJGDM");
            map.putAll(feiYongByGroup);
        }
        //获取分组费用
        return map;
    }

    @Override
    @Deprecated
    public List<HashMap<String, Object>> queryByParams(long id, Params params) throws Exception {
        //查询出参数进行简单拼接
        SqlTemplateInfo info = super.getById(id);
        StringBuilder sqlTemplate = new StringBuilder();
        DataProvider dataProvider = dataProviderManager.getDataProvider(info.getDataSourceConfigId());
        if (params.getQuYuCode() != null) {
            //TODO 关联区域代码
            // sqlTemplate.append(" and quyucode = "+params.getQuYuCode());
        }
        if (params.getStartTime() != null) {
            sqlTemplate.append(" and YWSCSJ > to_date('" + params.getStartTime() + "','yyyy-MM-dd')");
        } else {
            sqlTemplate.append(" and YWSCSJ > to_date('" + formatDate(getCurrYearFirst()) + "','yyyy-MM-dd')");
        }
        if (params.getEndTime() != null) {
            sqlTemplate.append(" and YWSCSJ < to_date('" + params.getEndTime() + "','yyyy-MM-dd')");
        } else {
            sqlTemplate.append(" and YWSCSJ < to_date('" + formatDate(getCurrYearLastDay()) + "','yyyy-MM-dd')");
        }
        if (params.getJiGouCode() != null) {
            sqlTemplate.append(" and YLJGDM = " + params.getJiGouCode());
        }
        sqlTemplate.append(" GROUP BY JZZDBM,JZZDMC ORDER BY  total desc" );
        String sql = "select * from ( "+info.getSqlTemplate()+sqlTemplate.toString()+") t  WHERE  rownum<11 ";
        System.out.println(sql);
        return  dataProvider.getData(sql);
    }

    @Override
    public List<HashMap<String, Object>> query(Long sqlTemplateInfoId) throws Exception {
        SqlTemplateInfo templateInfo = super.getById(sqlTemplateInfoId);
        DataProvider dataProvider = dataProviderManager.getDataProvider(templateInfo.getDataSourceConfigId());
        return dataProvider.getData(templateInfo.getSqlTemplate());
    }
}
